package com.xjscrm.server.common.util;

import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.common.utils.Tools;
import com.xjscrm.common.vo.sop.PlanData;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.Objects;

/**
 * @author lucher.chen create in 2024/3/1 18:25
 */
public class PlanDataTools {

    /**
     * SOP的计划数据获取执行时间
     *
     * @param planData 被动消息
     * @author lucher
     * @date 2024/3/6 15:43
     */
    public static Integer getSendTime(Integer firstTime, PlanData planData) {
        if (planData == null) {
            return null;
        }

        if (planData.getDay() == null) {
            return null;
        }

        Integer sendTime = null;

        String startTime = planData.getStartTime();
        if (StringUtils.isBlank(startTime)) {
            startTime = "00:00";
        }
        String endTime = planData.getEndTime();
        if (StringUtils.isBlank(endTime)) {
            endTime = "23:59";
        }
        //获取当前的日期
        LocalDate localDate = LocalDate.now();

        //第几天，如果是第一天，需要判断当前时间是否符合发送条件，如果符合，则今天发送，如果不符合，则明天的时间作为第一天
        Integer day = Tools.getInteger(planData.getDay());
        if (day.intValue() == 1) {//第一天需要验证今天时间是否在发送时间内
            if (Objects.equals(planData.getType(), 1)) {
                if (planData.getHour() == null) {
                    planData.setHour(0);
                }
                if (planData.getMinute() == null) {
                    planData.setMinute(1);
                }
                return DateUtil.getTime(new Date()) + planData.getHour() * 3600 + planData.getMinute() * 60;
            } else {
                // 获取当前时间
                LocalDateTime now_localDateTime = LocalDateTime.now();
                //判断开始时间是否还未轮到,则从今天开始，时刻为允许时间段内的随机时刻
                if(LocalTime.now().isBefore(LocalTime.parse(startTime))){
                    LocalDate localDate_today = LocalDate.now();
                    //获取随机时刻
                    LocalTime randomTime_today = TimeTools.getRandomTimeBetween(LocalTime.parse(startTime), LocalTime.parse(endTime));
                    Date date = TimeTools.getDateByLocalDayAndLocalTime(localDate_today, randomTime_today);
                    return DateUtil.getTime(date);
                }else{
                    if(LocalTime.now().isBefore(LocalTime.parse(endTime))){
                        LocalDate localDate_today = LocalDate.now();
                        //获取随机时刻
                        LocalTime randomTime_today = TimeTools.getRandomTimeBetween(LocalTime.now(), LocalTime.parse(endTime));
                        Date date = TimeTools.getDateByLocalDayAndLocalTime(localDate_today, randomTime_today);
                        return DateUtil.getTime(date);
                    }else {//从明天开始
                        LocalDate localDate_tomorrow = LocalDate.now().plusDays(1);
                        //获取随机时刻
                        LocalTime randomTime_tomorrow = TimeTools.getRandomTimeBetween(LocalTime.parse(startTime), LocalTime.parse(endTime));
                        Date date = TimeTools.getDateByLocalDayAndLocalTime(localDate_tomorrow, randomTime_tomorrow);
                        return DateUtil.getTime(date);
                    }
                }
            }
        } else {//不是第一天，则需要计算时间，第一次执行对于的日期+天数-1
            //获取第一天发送日期
            //如果首次执行时间没有，则默认当前时间
            if (firstTime == null) {
                firstTime = DateUtil.getTime();
            }
            Date date = new Date(firstTime * 1000L);
            LocalDate localDateFromDate = TimeTools.getLocalDateFromDate(date);
            //第一天发送的日期加上相隔天数
            long plusDay = planData.getDay().longValue() - 1; //减去第一天
            LocalDate sendTime_localDate = localDateFromDate.plusDays(plusDay);
            //获取随机时刻
            LocalTime randomTime = TimeTools.getRandomTimeBetween(LocalTime.parse(startTime), LocalTime.parse(endTime));
            Date sendTime_date = TimeTools.getDateByLocalDayAndLocalTime(sendTime_localDate, randomTime);
            return DateUtil.getTime(sendTime_date);
        }
    }

}
